//全局变量
//运行顺序
type="power";
Menu('power','name','count');
optionMenu('option','name','count');
Menu('ulshow',"高级管理员",'count');
//ajks
function Menu(type,name,count) {
	$.ajax({
		url: "power",
		type: "post",
		dataType: "JSON",
		data: {
            one:type,
            two:name,
            three:count,
		},
		success: function(data) {
            if(type=='ulshow'){
                ulshow(data);
            }else if(type=='addmune'){
               mueuenshow(data);
            }else if(type=="delmune"){
                mueuenshow(data);
            } else {
                show(data);
            }

	
			console.log(data);

		},
		error: function(err) {
			console.log(err)
		},
	});

}


//方法
function show(data){
    var select =document.getElementById("powername").innerHTML='';
for(i=0;i<data.length;i++){
    createselect(data[i].rOLE_NAME,data[i].rOLE_ID);
  }
}
function ulshow(data){
    var select =document.getElementById("show").innerHTML='';
for(i=0;i<data.length;i++){
    createuoshow(data[i].mUNE_NAME,data[i].mUNE_ID);
  }
}

//创建下拉框
function createselect(name,seq){
    var select =document.getElementById("powername");
    optionid=seq;
    var option =document.createElement("option");
    option.innerHTML=name;
    option.value=seq;
    select.appendChild(option);
}

function createuoshow(name,seq){
    var div =document.getElementById("show")
    var li =document.createElement('li');
    li.innerHTML=name;
    li.value=seq;
    div.appendChild(li);
}

function a(){
        var kd1 = document.getElementById('powername');
        var d1 = kd1.selectedIndex;
        var idnex1 = kd1.options[d1].innerHTML;
        Menu('ulshow',idnex1,'count');

}
function b(){
    //角色
    var kd1 = document.getElementById('powername');
    var d1 = kd1.selectedIndex;
    var idnex1 = kd1.options[d1].value;
    //菜单
    var kd2 = document.getElementById('power111');
    var d2 = kd2.selectedIndex;
    var idnex2 = kd2.options[d2].value;
    optionMenu('addmune',idnex1,idnex2);
}

function  mueuenshow(){
    document.getElementById("lishow").innerHTML='';
    var kd1 = document.getElementById('powername');
    var d1 = kd1.selectedIndex;
    var idnex1 = kd1.options[d1].value;
    Menu('ulshow',idnex1,'count');
}
function c(){
    //角色
    var kd1 = document.getElementById('powername');
    var d1 = kd1.selectedIndex;
    var idnex1 = kd1.options[d1].value;
    //菜单
    var kd2 = document.getElementById('power');
    var d2 = kd2.selectedIndex;
    var idnex2 = kd2.options[d2].value;
    Menu('delmune',idnex1,idnex2);
}
function optionshow(data){
    for(i=0;i<data.length;i++){
        Musenshow(data[i].mUNE_NAME,data[i].mUNE_ID);
    }
}
// 创建下拉框
function Musenshow(munename,seq){
    var select =document.getElementById("power111");
    optionid=seq;
    var option =document.createElement("option");
    option.innerHTML=munename;
    option.value=seq;
    select.appendChild(option);
}

function optionMenu(type,name,count) {
    $.ajax({
        url: "power",
        type: "post",
        dataType: "JSON",
        data: {
            one: type,
            two: name,
            three: count,
        },
        success: function (data) {
            if (type=="del"){
               if(data===false){
                   alert("不可以对管理员进行操作");
               }
                a();
            }else if(type=="addmune"){
                a();
            }
            else {
                optionshow(data);
            }

            console.log(data);
        },
        error: function (err) {
            console.log(err)
        },
    });
}

function  optiondel(){
    var kd1 = document.getElementById('powername');
    var d1 = kd1.selectedIndex;
    var idnex1 = kd1.options[d1].value;
    //菜单
    var kd2 = document.getElementById('power111');
    var d2 = kd2.selectedIndex;
    var idnex2 = kd2.options[d2].value;
    optionMenu('del',idnex1,idnex2);
}